﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Resources;
using EnvDTE;
using EnvDTE80;
using Microsoft.VSPowerToys.ResourceRefactor.Common;
using XamlLocalizer.Services;

namespace XamlLocalizerAddin.ViewModels
{
    public class ResourceHandler : IResourceHandler
    {        
        private readonly DTE2 applicationObject;

        public ResourceHandler(DTE2 applicationObject)
        {
            this.applicationObject = applicationObject;
        }

        private ILanguageStrategy GetActiveProjectLanguageStrategy()
        {
            Project project = applicationObject.ActiveDocument.ProjectItem.ContainingProject;
            
            switch (project.CodeModel.Language)
            {
                case CodeModelLanguageConstants.vsCMLanguageCSharp:
                    return new CSharpLanguageStrategy();
                case CodeModelLanguageConstants.vsCMLanguageVB:
                    return new VBLanguageStrategy();
                default:
                    throw new NotSupportedException("Language of current project is not supported");
            }
        }


        public string GetResourcesNamespace()
        {
            Project project = applicationObject.ActiveDocument.ProjectItem.ContainingProject;
            var rootNamespace = project.Properties.Item("RootNamespace").Value.ToString();
            return rootNamespace + ".Properties";
        }

        public string GetResourcesFolder()
        {
            Project project = applicationObject.ActiveDocument.ProjectItem.ContainingProject;
            string projDir = Path.GetDirectoryName(project.FileName);
            string propsDir = Path.Combine(projDir, "Properties");
            return propsDir;
        }

        public List<IResourceFileDesc> GetResourceFileList()
        {
            Project project = applicationObject.ActiveDocument.ProjectItem.ContainingProject;

            var resourceFilesList = new List<IResourceFileDesc>();

            var resourceFileCollection = new ResourceFileCollection(project, IsValidResourceFile);
            foreach (var resFile in resourceFileCollection)
            {
                resourceFilesList.Add(new ResourceFileDesc(resFile));
            }
            return resourceFilesList;
        }

        private static bool IsValidResourceFile(ProjectItem item)
        {
            if (item == null)
            {
                return false;
            }
            try
            {
                return (item.Properties.Item("Extension").Value.ToString().Equals(".resx", StringComparison.InvariantCultureIgnoreCase)
                    && (item.Properties.Item("ItemType").Value.Equals("EmbeddedResource")
                    && (item.Properties.Item("CustomTool").Value.Equals("ResXFileCodeGenerator")
                        || item.Properties.Item("CustomTool").Value.Equals("PublicResXFileCodeGenerator"))));
            }
            catch (ArgumentException)
            {
                return false;
            }
        }

        public IResourceFileDesc CreateResourceFileDesc(string resFileName)
        {
            return new ResourceFileDesc(resFileName);
        }

        public void CreateNewResFile(IResourceFileDesc resFileDesc)
        {
            var resFile = (ResourceFileDesc)resFileDesc;

            using (var writer = new ResXResourceWriter(resFile.FileName))
            {
                writer.Generate();
            }
            Project project = applicationObject.ActiveDocument.ProjectItem.ContainingProject;
            ProjectItem newProjectItem = project.ProjectItems.AddFromFile(resFile.FileName);
            UpdateResourceFileProperties(newProjectItem);
            var resourceFile = new ResourceFile(newProjectItem);
            resFile.ExistingFile = resourceFile;
        }

        private static void UpdateResourceFileProperties(ProjectItem item)
        {
            if (item != null)
            {
                item.Properties.Item("CustomTool").Value = "PublicResXFileCodeGenerator";
                item.Properties.Item("ItemType").Value = "EmbeddedResource";
            }
        }

        public void AddCSharpFile(string fileName)
        {
            Project project = applicationObject.ActiveDocument.ProjectItem.ContainingProject;
            ProjectItem newProjectItem = project.ProjectItems.AddFromFile(fileName);
        }
    }

    public interface ILanguageStrategy
    {
        
    }

    public class CSharpLanguageStrategy : ILanguageStrategy
    {
        
    }

    public class VBLanguageStrategy : ILanguageStrategy
    {

    }
}
